package com.fcscs.cservice.client.main.iguestusers;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.IconButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.fcscs.core.common.MainPaginationSearchModel;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreUtil;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.SearchExecutor;
import com.fcscs.core.gwt.common.ui.FcsFields.HighLightIconButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel;
import com.fcscs.core.gwt.common.ui.PagingSearchResultsPanel.ModelSelectListener;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.controller.CServiceCommonServiceController;
import com.fcscs.cservice.client.common.controller.ReservationServiceController;
import com.fcscs.cservice.client.common.model.iguestusers.iGuestUsersModel;
import com.fcscs.cservice.client.common.model.iguestusers.iGuestUsersModelData;
import com.fcscs.cservice.client.main.reservation.FolioDialog;
import com.fcscs.cservice.domain.model.FolioView;
import com.fcscs.share.gwt.common.ShareGWTConstants;

	public class iGuestUsersGrid extends
	PagingSearchResultsPanel<iGuestUsersModelData, MainPaginationSearchModel> implements
	FormSubmitListener<iGuestUsersModel>, ModelSelectListener<iGuestUsersModelData> {
	private iGuestUsersDetailDialog iguestUsersDetailDialog;
	
	public iGuestUsersGrid(String title, SearchExecutor<iGuestUsersModelData, MainPaginationSearchModel> exe) {
	super(title, exe, (Object[]) null);
	setSize(958, 400);
	}
	
	@Override
	public void onModelClick(iGuestUsersModelData model) {
	
	}
	
	@Override
	public void onModelDoubleClick(iGuestUsersModelData model) {
	
	}
	
	@Override
	public void onModelEnterPress(iGuestUsersModelData model) {
	
	}
	
	@Override
	public void formSubmitCallBack(FcsFormPanel<iGuestUsersModel> form, SubmitType submitType) {
	
	}
	
	@Override
	protected List<ColumnConfig> getColumnConfig(Object... params) {
	List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
	ColumnConfig cc = CoreUtil.createNonSortableColumnConf(CServiceConstants.GUESTNAME,
			CServiceConstants.CSERVICE_CONSTANTS.guestName(),150);
	columns.add(cc);
	
	cc = CoreUtil.createNonSortableColumnConf(ShareGWTConstants.ROOM,
			ShareGWTConstants.SHARE_GWT_CONSTANTS.roomNo(), 120);
	columns.add(cc);
	
	columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.ACTIVATIONDATETIME,
			CServiceConstants.CSERVICE_CONSTANTS.Activation(), 150));
	columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.DEPARTUREDATETIME,
			CServiceConstants.CSERVICE_CONSTANTS.Departure(), 150));
	
	columns.add(CoreUtil.createNonSortableColumnConf(CServiceConstants.STATUS,
			CoreConstants.CORE_CONSTANTS.status(), 100));
	cc = CoreUtil.createNonSortableColumnConf(CoreConstants.ACTION,
			CoreConstants.CORE_CONSTANTS.action(), 80);
	
	cc.setRenderer(new GridCellRenderer<iGuestUsersModelData>() {
	
		@Override
		public Object render(iGuestUsersModelData model, String property, ColumnData config,
				int rowIndex, int colIndex, ListStore<iGuestUsersModelData> store,
				Grid<iGuestUsersModelData> grid) {
			HorizontalPanel hr = new HorizontalPanel();
			hr.setHorizontalAlign(HorizontalAlignment.LEFT);
	
			HighLightIconButton ib = createProfileBtn(model);
			ib.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "3px");
			ib.setId("PayTypeGrid_profile_button");
			hr.add(ib);
			
			//requestlist icon
//			HighLightIconButton ir = createRequestListBtn(model);
//			ir.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "3px");
//			ir.setId("PayTypeGrid_requestlist_button");
//			hr.add(ir);
			return hr;
		}
	});
	columns.add(cc);
	return columns;
	}
	
	private HighLightIconButton createProfileBtn(final iGuestUsersModelData model) {
	HighLightIconButton iconBtn = new HighLightIconButton(CoreConstants.CSS_RUNNERPROFILE_SM_ICON,
			new SelectionListener<IconButtonEvent>() {
		@Override
		public void componentSelected(IconButtonEvent ce) {
//			showProfileDeviceDialog(model);
			

//			if(model.getEntity().getId() != null){
//			CServiceCommonServiceController.getINSTANCE().getIGuestUserProfileView(callback) .get(
//					model.getEntity().getFolioId(), 
//					new AsyncCallbackListener<FolioView>(){
//						@Override
//						public void onLogin(String username) {
//							ReservationServiceController.getINSTANCE().getFolioView(
//									model.getEntity().getFolioId(),this);
//						}
//
//						@Override
//						public void onFailure(Throwable caught) {
//							MessageBox.alert(
//									CoreConstants.CORE_MESSAGES.operation(
//										CoreConstants.CORE_CONSTANTS.system(), 
//										ShareGWTConstants.SHARE_GWT_CONSTANTS.error()),
//									CoreConstants.CORE_CONSTANTS.systemError(), 
//									null);
//						}
//
//						@Override
//						public void onSuccess(FolioView result) {
//							foliodialog = FolioDialog.getINSTANCE(reservConf);
//							foliodialog.getFormPanel().updateForm(result);
//							foliodialog.show();
//						}
//						
//					});
//		}else{
//			MessageBox.alert(
//					"",
//					CServiceConstants.CSERVICE_MESSAGES.thereIsNo(CServiceConstants.CSERVICE_CONSTANTS.folio()), 
//					null);
//		}
		
			
		}
	});
	iconBtn.setId("DeviceGridPanel_create_profilebtn_id");
	iconBtn.setTitle(CoreConstants.CORE_CONSTANTS.profile());
	return iconBtn;
	}
	
	private void showProfileDeviceDialog(iGuestUsersModelData model) {
		iguestUsersDetailDialog = iGuestUsersDetailDialog.getInstance();
		iguestUsersDetailDialog.getFormPanel().updateForm(model.getEntity());
		iguestUsersDetailDialog.show();
		
		
	}
//Them vao 25/06
	private HighLightIconButton createRequestListBtn(final iGuestUsersModelData model) {
		HighLightIconButton iconBtn = new HighLightIconButton(CoreConstants.CSS_GUESTREQUEST_SM_ICON,
				new SelectionListener<IconButtonEvent>() {
			@Override
			public void componentSelected(IconButtonEvent ce) {
				showProfileDeviceDialog(model);
			}
		});
		iconBtn.setId("DeviceGridPanel_create_profilebtn_id");
		iconBtn.setTitle(CoreConstants.CORE_CONSTANTS.requestlist());
		return iconBtn;
		}
}

